package com.example.myapplication.found;

import com.example.myapplication.found.bean.BuyPointBean;
import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.found.bean.KDJOrderBean;
import com.example.myapplication.found.bean.StockOrderBean;
import com.example.myapplication.found.bean.ZhangDieBean;
import com.example.myapplication.found.contract.KdjLoopCallback;
import com.example.myapplication.found.contract.SimpleCallback;
import com.example.myapplication.found.contract.ZiHangCallback;
import com.example.myapplication.found.model.Fenxi;
import com.example.myapplication.found.model.KDJ;
import com.example.myapplication.found.model.Point;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2022/2/10
 * @description
 */
public class IndustryList {
    List<FoundBean> foundList = new ArrayList<>();
    ExecutorService pool;
    int index;
    String code;
    String name;
    String date;
    public float money;
    private KdjLoopCallback mKdjLoopCallback;
    private ZiHangCallback ziHangCallback;

    StockOrderBean orderBean;

    public IndustryList(String code, String name, String date) {
        pool = Executors.newFixedThreadPool(5);
        this.code = code;
        this.name = name;
        this.date = date;
    }

    public static void main(String[] args) {
        IndustryList industryList = new IndustryList("BK0450", "测试", "2022-03-02");
        industryList.getStockList();
    }

    private void start() {
        testModel();
    }

    private void testModel() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {
                if (index < foundList.size()) {
                    FoundBean item = foundList.get(index);
                    Stock2 stock = new Stock2(item.f12, item.f14, item.f13);
                    stock.setSimpleCallback(simpleCallback);
                    stock.date = date;
                    stock.start();
                    index++;
                } else {
//                    System.out.println("结束");
                    analysis1();
                }
            }
        });
    }

    public void analysis1() {


//        if (buyPointBeanList.size() > 0) {
//            System.out.println("行业 " + code + " " + name);
//            for (BuyPointBean pointBean : buyPointBeanList) {
//                System.out.println("个股买点 " + pointBean.date + " " + pointBean.name + " " + pointBean.code);
//                System.out.println("周  j=" + pointBean.weekKdj.j + " k=" + pointBean.weekKdj.k + " d=" + pointBean.weekKdj.d);
//                System.out.println("日  j=" + pointBean.dayKdj.j + " k=" + pointBean.dayKdj.k + " d=" + pointBean.dayKdj.d);
//                System.out.println("股价 " + pointBean.shouPan);
//                System.out.println();
//            }
//        }

        if(ziHangCallback!=null){
            ziHangCallback.ziHangCallback(industryOrderList);
        }
        if(orderBean!=null){
            orderBean.childOrderBeanList = industryOrderList;
        }


        if (mKdjLoopCallback != null) {
            mKdjLoopCallback.kdjLoopCallback("");
        }
    }


    public void setKdjCallback(KdjLoopCallback kdjCallback) {
        this.mKdjLoopCallback = kdjCallback;
    }

    public void setZiHangCallback(ZiHangCallback callback){
        this.ziHangCallback = callback;
    }

    List<KDJOrderBean> industryOrderList = new ArrayList<>();
    List<BuyPointBean> buyPointBeanList = new ArrayList<>();

    SimpleCallback simpleCallback = new SimpleCallback() {
        @Override
        public void fenxiCallback(int type) {

        }

        @Override
        public void daBanCallback() {

        }

        @Override
        public void cghCallback(Map<Integer, Float> map,  KDJOrderBean kdjOrderBean) {

        }

        @Override
        public void zhangDieCallback(ZhangDieBean zhangDieBean) {

        }

        @Override
        public void kdjCallback(KDJOrderBean kdjOrderBean, BuyPointBean buyPointBean) {
            if (kdjOrderBean != null) {
                industryOrderList.add(kdjOrderBean);
            }
            if (buyPointBean != null) {
                buyPointBeanList.add(buyPointBean);
            }
            testModel();
        }

        @Override
        public synchronized void pointCallback(List<StockOrderBean> orderList) {

        }


        @Override
        public void testModelNext(float money) {

        }
    };


    public void setOrderBean(StockOrderBean orderBean) {
        this.orderBean = orderBean;
    }


    public void getStockList() {
        String url_hangye_list = "https://72.push2.eastmoney.com/api/qt/clist/get?cb=jQuery11240797026970623131_1646049374402&pn=1&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=b:%s+f:!50&fields=f12,f13,f14&_=1646049374414";
        String url = String.format(url_hangye_list, code);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String tag1 = "\"diff\":";
                    int index1 = html.indexOf(tag1);
                    int index2 = html.lastIndexOf("}});");
                    html = html.substring(index1 + tag1.length(), index2);
                    List<FoundBean> bean = GsonUtil.parse(html, new TypeToken<List<FoundBean>>() {
                    }.getType());
                    foundList = bean;
                    start();
                }
            }
        });
    }
}
